package com.htl.集合;

import java.util.TreeSet;

public class TreeSet__test2 {
    public static void main(String[] args) {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new B(1));
        treeSet.add(new B(3));
        treeSet.add(new B(6));
        treeSet.add(new B(2));
        treeSet.add(new B(5));
        treeSet.add(new B(4));
        treeSet.add(new B(1));
        System.out.println(treeSet);
    }
}

class B implements Comparable{
    private int num;
    public B(int num) {
        this.num = num;
    }
    @Override
    public int compareTo(Object o) {
        /**
         * A.compareTo(B)
         * 1表示 A大于B
         * 0表示 A等于B
         * -1表示 A小于B
         */
        B b = (B) o;
        if(this.num > b.num) return 1;
        if(this.num == b.num) return 0;
        if(this.num < b.num) return -1;
        return 0;
    }
    @Override
    public String toString() {
        return "B{" +
                "num=" + num +
                '}';
    }
}
